//
//  RegisterViewController.m
//  Newproject3
//
//  Created by rimi on 16/9/12.
//  Copyright © 2016年 Liang. All rights reserved.
//

#import "RegisterViewController.h"

@interface RegisterViewController ()
@property (nonatomic, strong) UITextField *username;
@property (nonatomic, strong) UITextField *password;
@property (nonatomic, strong) UITextField *againPassword;
@property (nonatomic, strong) UIButton *sureButton;
@property (nonatomic,strong)UITextField *name;
@property (nonatomic,strong)NSMutableArray *userArray;

@end

@implementation RegisterViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self.leftButton setImage:[UIImage imageNamed:@"个人中心-left@2x"] forState:UIControlStateNormal];
    self.view.backgroundColor = [UIColor colorWithWhite:0.883 alpha:1.000];
    [self initUi];
}

-(void)initUi{
    self.centerLabel.text = @"注册";
    [self.view addSubview:self.username];
    [self.view addSubview:self.password];
    [self.view addSubview:self.againPassword];
    [self.view addSubview:self.sureButton];
    [self.view addSubview:self.name];

}

// 隐藏下面的分栏标签，进入这个界面的时候隐藏，离开界面的时候显示
-(void)viewWillAppear:(BOOL)animated{
    self.navigationController.tabBarController.tabBar.hidden = YES;
}

-(void)viewWillDisappear:(BOOL)animated{
    self.navigationController.tabBarController.tabBar.hidden = NO;
}


-(void)sureButton_cation{
    // 非正常注册
    if (self.username.text.length == 0 || self.password.text != self.againPassword.text || self.name.text.length == 0 || self.password.text.length == 0 || self.againPassword.text.length == 0) {
        
        UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"提示" message:@"用户名或密码错误" preferredStyle:UIAlertControllerStyleActionSheet];
        
        UIAlertAction *photoAction = [UIAlertAction actionWithTitle:@"确认" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        }];
        
        [alertController addAction:photoAction];
        [self presentViewController:alertController animated:YES completion:nil];
        return;
    }
    // 正常注册
    if (self.username.text.length != 0 && self.password.text == self.againPassword.text && self.name.text.length != 0) {
        // 没有注册历史
        if (![[NSUserDefaults standardUserDefaults] objectForKey:@"userInfo"]) {
            
            UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"提示" message:@"注册成功" preferredStyle:UIAlertControllerStyleActionSheet];
            
            UIAlertAction *photoAction = [UIAlertAction actionWithTitle:@"确认" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            }];
            
            [alertController addAction:photoAction];
            [self presentViewController:alertController animated:YES completion:nil];
            
            NSDictionary *dict = @{@"username":self.username.text,@"password":self.password.text,@"nicheng":self.name.text};
            
            [self.userArray addObject:dict];
            
            [[NSUserDefaults standardUserDefaults] setObject:self.userArray forKey:@"userInfo"];
            
            dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                [self.navigationController popViewControllerAnimated:YES];
            });
            
        }else{
            // 有注册历史
            __block BOOL isRegist = false;
           [[[NSUserDefaults standardUserDefaults] objectForKey:@"userInfo"] enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
               if ([self.username.text isEqualToString:obj[@"username"]] || [self.name.text isEqualToString:obj[@"nicheng"]]) {
                   //用户名或者昵称已存在
                   UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"提示" message:@"用户名或者昵称已存在" preferredStyle:UIAlertControllerStyleActionSheet];
                   
                   UIAlertAction *photoAction = [UIAlertAction actionWithTitle:@"确认" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
                   }];
                   
                   [alertController addAction:photoAction];
                   [self presentViewController:alertController animated:YES completion:nil];
                   isRegist = true;
                   *stop = true;
               }
           }];

            if (!isRegist) {
                NSDictionary *dict = @{@"username":self.username.text,@"password":self.password.text,@"nicheng":self.name.text};
                
                [self.userArray addObject:dict];
                
                [[NSUserDefaults standardUserDefaults] setObject:self.userArray forKey:@"userInfo"];
                
                [[NSUserDefaults standardUserDefaults] synchronize];
                UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"提示" message:@"注册成功" preferredStyle:UIAlertControllerStyleActionSheet];
                
                UIAlertAction *photoAction = [UIAlertAction actionWithTitle:@"确认" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
                }];
                
                [alertController addAction:photoAction];
                [self presentViewController:alertController animated:YES completion:nil];
            }
            
        }
    
    }
    
}

#pragma mark - getter
- (UITextField *)username{
    if (!_username) {
        _username = [[UITextField alloc] initWithFrame:CGRectMake(kwidth * 0.02, khieght * 0.2, kwidth * 0.9, khieght * 0.06)];
        _username.placeholder = @"请填写用户名:";
        _username.clearButtonMode = UITextFieldViewModeWhileEditing;
        _username.borderStyle = UITextBorderStyleBezel;
    }
    return _username;
}

- (UITextField *)password{
    if (!_password) {
        _password = [[UITextField alloc] initWithFrame:CGRectMake(kwidth * 0.02, khieght * 0.28, kwidth * 0.9, khieght * 0.06)];
        _password.placeholder = @"请输入密码:";
        _password.secureTextEntry = YES;
        _password.clearButtonMode = UITextFieldViewModeWhileEditing;
        _password.borderStyle = UITextBorderStyleBezel;
    }
    return _password;
}

- (UITextField *)againPassword{
    if (!_againPassword) {
        _againPassword = [[UITextField alloc] initWithFrame:CGRectMake(kwidth * 0.02, khieght * 0.36, kwidth * 0.9, khieght * 0.06)];
        _againPassword.secureTextEntry = YES;
        _againPassword.placeholder = @"确认密码:";
        _againPassword.clearButtonMode = UITextFieldViewModeWhileEditing;
        _againPassword.borderStyle = UITextBorderStyleBezel;
    }
    return _againPassword;
}

-(UITextField *)name{
    if (_name == nil) {
        _name = [[UITextField alloc] initWithFrame:CGRectMake(kwidth * 0.02, khieght * 0.44, kwidth * 0.9, khieght * 0.06)];
        _name.borderStyle = UITextBorderStyleBezel;
        _name.clearButtonMode = UITextFieldViewModeWhileEditing;
        _name.placeholder = @"请输入昵称:";

    }
    return _name;
}

- (UIButton *)sureButton{
    if (!_sureButton) {
        _sureButton = [[UIButton alloc] initWithFrame:CGRectMake(kwidth * 0.02, khieght * 0.56, kwidth * 0.9, khieght * 0.06)];
        _sureButton.backgroundColor = [UIColor colorWithRed:0.000 green:0.467 blue:0.965 alpha:1.000];
        [_sureButton setTitle:@"let's go" forState:UIControlStateNormal];
        [_sureButton addTarget:self action:@selector(sureButton_cation) forControlEvents:UIControlEventTouchUpInside];
    }
    return _sureButton;
}

-(NSMutableArray *)userArray{
    if (_userArray == nil) {
        // 本地如果有数据就拿数据
        if ([[NSUserDefaults standardUserDefaults] objectForKey:@"userInfo"]) {
            _userArray = [[[NSUserDefaults standardUserDefaults] objectForKey:@"userInfo"] mutableCopy];
        }else{
            _userArray = [NSMutableArray array];
        }
    }
    return _userArray;
}

@end
